package cn.kgc.ssm.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.ssm.commons.ResponseResult;
import cn.kgc.ssm.entity.Slideshow;
import cn.kgc.ssm.service.SlidShowService;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 课工场
 * @date 2024/5/16
 * @description   轮播区
 */
@Controller
@RequestMapping("slide")
public class SlidShowController {


    @Resource
    private SlidShowService slidShowService;


    // 后台轮播数据分页
    @RequestMapping("page")
    @ResponseBody
    public ResponseResult  slidePage(Integer page,Integer limit){
        return slidShowService.slidePage(page,limit);
    }


    //  轮播图置顶
    @RequestMapping("edit")
    @ResponseBody
    public ResponseResult  editSlide(Integer id){
        return slidShowService.editSlide(id);
    }

    // 删除轮播图
    @RequestMapping("delete")
    @ResponseBody
    public ResponseResult  deleteSlide(Integer id,String uuid){
        return slidShowService.deleteSlide(id,uuid);
    }



    // 前端轮播图列表   返回前五条
    @RequestMapping("front/list")
    @ResponseBody
    public ResponseResult  slidePictureList(){
        return slidShowService.getslidePictureList();
    }


    // 获取图片
    @RequestMapping("front/getSlidePicture")
    public  void   slidePictureList(String uuid, HttpServletResponse response) throws Exception {

        // 根据图片的名字 获取图片   D:/upload/slide/uuid
        File file = new File("D:/upload/slides/" + uuid);
        FileInputStream fileInputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();

        IOUtils.copy(fileInputStream,outputStream);
        fileInputStream.close();
        outputStream.close();
    }


    // 轮播图片上传
    @RequestMapping("upload")
    @ResponseBody
    public ResponseResult  uploadPicture(MultipartFile  file) throws IOException {

        // 判定文件是否是空文件
        if (file.isEmpty()){
           return   ResponseResult.fail().message("请选择文件");
        }

        // 文件上传  test.png
        String originalFilename = file.getOriginalFilename();
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);  //  png  jpg
        // String replace = UUID.randomUUID().toString().replace("-", "");
        String uuid = IdUtil.simpleUUID();
        //   文件在服务器中的唯一的标识
        String lastName = uuid+"."+filenameExtension;

        File file1 = new File("D:/upload/slides/");
        if (!file1.exists()){
            file1.mkdir();
        }

        File target = new File(file1, lastName);
        file.transferTo(target);

        return ResponseResult.success().data(lastName);
    }


    // 轮播图基本描述信息添加   uploadInfo
    @RequestMapping("uploadInfo")
    @ResponseBody
    public ResponseResult   addUploadInfo(Slideshow slideshow){

        return  slidShowService.addUploadInfo(slideshow);
    }
}
